package com.woniuxy.woniuhis.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.ExaminationResult;
import com.woniuxy.woniuhis.service.ExaminationResultService;
import com.woniuxy.woniuhis.util.ResponseJson;
import com.woniuxy.woniuhis.util.UUIDUtil;

@Controller
@RequestMapping("/examinationresult")
public class ExaminationResultController {
	
	@Autowired
	ExaminationResultService examinationResultService;
	
	// 查询所有检查结果单
	@RequestMapping("/all")
	@ResponseBody
	public ResponseJson<ExaminationResult> getAllExaminationResult(@RequestBody ExaminationResult examinationResult) {
		ResponseJson<ExaminationResult> responseJson = new ResponseJson<ExaminationResult>();
		try {
			if(examinationResult == null) {
				examinationResult = new ExaminationResult();
			}
			Integer page = examinationResult.getPage();
			Integer limit = examinationResult.getLimit();
			if(page == null) {
				page = 1;
			}
			if(limit == null) {
				limit = 10;
			}
			PageInfo<ExaminationResult> examinationResults = examinationResultService.findExaminationResultByCondition(examinationResult,page,limit);
			responseJson.setData(examinationResults.getList());
			responseJson.setCount((int)examinationResults.getTotal());
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	// 添加检查结果单
	@RequestMapping("/add")
	@ResponseBody
	public ResponseJson addAllExaminationResult(@RequestBody ExaminationResult examinationResult) {
		ResponseJson responseJson = new ResponseJson();
		try {
			
			if(examinationResult == null) {
				throw new RuntimeException("提交的检查结果单为空");
			}
			examinationResult.setExamResultId(UUIDUtil.uuid32());
			examinationResult.setExamResultTime(new Date());
			System.out.println("ExaminationResultController:"+examinationResult);
			examinationResultService.addExaminationResult(examinationResult);
			responseJson.setMsg("添加成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
	
	// 更新检查结果单
	@RequestMapping("/update")
	@ResponseBody
	public ResponseJson updateExaminationResult(@RequestBody ExaminationResult examinationResult) {
		ResponseJson responseJson = new ResponseJson();
		try {
			
			if(examinationResult == null) {
				throw new RuntimeException("提交的检查结果单为空");
			}
			if(examinationResult.getExamResultId() == null || "".equals(examinationResult.getExamResultId())) {
				throw new RuntimeException("提交的检查结果单编号不能为空");
			}
			ExaminationResult examResult = examinationResultService.findExaminationResultByid(examinationResult.getExamResultId());
			if(examResult == null ) {
				throw new RuntimeException("提交的检查结果单编号不正确");
			}
			examinationResultService.modifyExaminationResultById(examinationResult);
			System.out.println(examinationResult);
			responseJson.setMsg("修改成功");
			responseJson.setCode(0);
			return responseJson;
		} catch (Exception e) {
			responseJson.setCode(1);
			responseJson.setMsg(e.getMessage());
			return responseJson;
		}
	}
}
